package com.yexuejc.vertx.todolist.verticle;

import com.yexuejc.vertx.todolist.consts.Constants;
import com.yexuejc.vertx.todolist.consts.ServiceAddress;
import io.vertx.core.Future;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.jdbc.JDBCClient;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.ext.web.handler.CorsHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;

/**
 * @PackageName: com.yexuejc.vertx.todolist.entity
 * @Description:
 * @author: maxf
 * @date: 2018/4/12 17:01
 */
public class ApplicationVerticle extends RestfulApiVerticle {
    private static Logger logger = LoggerFactory.getLogger(ApplicationVerticle.class);

    @Override
    public void start(Future<Void> startFuture) throws Exception {
        Router router = Router.router(vertx);
        //开启body处理
        router.route().handler(BodyHandler.create());
        //开启CORS
        enableCorsSupport(router);


        //基于RESTFul的api
        router.get(Constants.API_GET).handler(this::handleGetTodo);
        router.get(Constants.API_LIST_ALL).handler(this::handleGetAll);
        router.post(Constants.API_CREATE).handler(this::handleCreateTodo);
        router.patch(Constants.API_UPDATE).handler(this::handleUpdateTodo);
        router.delete(Constants.API_DELETE).handler(this::handleDeleteOne);
        router.delete(Constants.API_DELETE_ALL).handler(this::handleDeleteAll);

        //配置
        vertx.createHttpServer().requestHandler(router::accept).listen(8080, it -> {
            if (it.succeeded()) {
                System.out.println("启动成功");
            } else {
                System.out.println("启动失败");
            }
        });
    }

    private void handleUpdateTodo(RoutingContext context) {
    }

    private void handleDeleteOne(RoutingContext context) {
    }

    private void handleDeleteAll(RoutingContext context) {
    }

    private void handleCreateTodo(RoutingContext context) {
    }

    /**
     * 根据id获取
     *
     * @param context
     */
    private void handleGetTodo(RoutingContext context) {
        String todoID = context.request().getParam("todoId");
        if (todoID == null) {
            badRequest(context, "参数为空");
            return;
        }
        sendResponse(context, ServiceAddress.GET_TODO, todoID);
    }

    private void handleGetAll(RoutingContext context) {

    }

    /**
     * Enable CORS support for web router.
     *
     * @param router router instance
     */
    protected void enableCorsSupport(Router router) {
        Set<String> allowHeaders = new HashSet<>();
        allowHeaders.add("x-requested-with");
        allowHeaders.add("Access-Control-Allow-Origin");
        allowHeaders.add("origin");
        allowHeaders.add("Content-Type");
        allowHeaders.add("accept");
        // CORS support
        router.route().handler(CorsHandler.create("*")
                .allowedHeaders(allowHeaders)
                .allowedMethod(HttpMethod.GET)
                .allowedMethod(HttpMethod.POST)
                .allowedMethod(HttpMethod.DELETE)
                .allowedMethod(HttpMethod.PATCH)
                .allowedMethod(HttpMethod.PUT)
        );
    }

}
